package Lono::Recipe::Writer::Socialtext;

use strict;
use warnings;

=head1 NAME

  Lono::Recipe::Writer::Socialtext -

=head1 SYNOPSIS

=head1 DESCRIPTION

=cut

use Socialtext::Resting;

use Lono::Recipe::Renderer::Wiki;

use constant PAGE_TAG => 'testrecipe';

sub new {
    my ( $class, $config ) = @_;

    my $rester = Socialtext::Resting->new(
        'username' => $config->{'username'},
        'password' => $config->{'password'},
        'server'   => $config->{'server'},
    );
    $rester->workspace( $config->{'workspace'} );

    my $self = { '_rester' => $rester,
                 '_config' => $config };
    bless $self, $class;
    return $self;
}

sub send {
    my ( $self, $recipe ) = @_;
    my $wikitext = Lono::Recipe::Renderer::Wiki->render( $recipe );
    my $page = "TEST " . $recipe->get_name;

    $self->{'_rester'}->put_page( $page, $wikitext );
    $self->{'_rester'}->put_pagetag( $page, PAGE_TAG );
    
    # Is this just saying whether or not adding the tag was successful?
    my $response = $self->{'_rester'}->response;
    return $response->is_success;
}

1;
